//
//  ReadFile.m
//  MyGameSetting
//
//  Created by Hunk on 10-5-7.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ReadFile.h"


@implementation ReadFile

-(id)init
{
	if((self = [super init]))
	{
		readData = NULL;
		m_nRange = 0;
	}
	return self;
}

// Open file before read it's data
-(BOOL)OpenFile:(NSString*)filePath
{
	NSArray *arrayPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	
	NSString *documentsDirectory = [arrayPath objectAtIndex:0];
	
	//文件路径或者直接用~表示当前应用的目录
	NSString *strFileName = [NSString alloc];
	strFileName = filePath;
	
	NSString *strFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName]; 
	
	readData = [NSData dataWithContentsOfFile:strFilePath];
	return readData != NULL;
}

// Close File
-(void)CloseFile
{
	if (readData)
	{
		readData = nil;
		[readData release];
	}
}

// Read int data to (int *)buf from file
-(void)ReadInt:(int*)buf
{
	[readData getBytes:buf range:NSMakeRange(m_nRange, sizeof(int))];
	m_nRange += sizeof(int);
}

// Read float data
-(void)ReadFloat:(float*)buf
{
	[readData getBytes:buf range:NSMakeRange(m_nRange, sizeof(float))];
	m_nRange += sizeof(float);
}

// Read String data 
-(NSString*)ReadString//:(NSString*)str bufLength:(int)length
{
	NSString* str = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
	
	NSString* string = str;
	
	[str release];
	
	return string;
}

// Read Data
-(void)ReadData:(NSString*)strData size:(int)size
{
	[readData getBytes:strData range:NSMakeRange(m_nRange, size)];
	m_nRange += size;
}

-(void)dealloc
{
	if (readData)
	{
		[readData release];
		readData = nil;
	}
	
	[super dealloc];
}

@end
